package org.infodavid.common.model.impl;

import org.infodavid.common.model.IUser;
import org.infodavid.common.model.IUserConnection;

/**
 * The Class UserConnectionImpl.
 */
public class UserConnectionImpl extends AbstractImprovedDataObjectImpl<Long> implements
    IUserConnection {

	/** serialVersionUID. */
	private static final long serialVersionUID = -1478066595640761601L;

	/** The approval status. */
	private EApprovalStatus approvalStatus;

	/** The comment. */
	private String comment;

	/** The connection as user. */
	private IUser recipient;

	/** The user. */
	private IUser initiator;

	/**
	 * constructor.
	 * @throws Exception the exception
	 */
	protected UserConnectionImpl() throws Exception {
		super();
	}

	/**
	 * constructor.
	 * @param keyArg the key
	 * @throws Exception the exception
	 */
	protected UserConnectionImpl(final Long keyArg) throws Exception {
		super();

		setKey(keyArg);
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.impl.DataObjectImpl#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}

		if (obj == null) {
			return false;
		}
		if (!(obj instanceof IUserConnection)) {
			return false;
		}

		final IUserConnection other = (IUserConnection)obj;

		if (getRecipient() == null) {
			if (other.getRecipient() != null) {
				return false;
			}
		}
		else if (!getRecipient().equals(other.getRecipient())) {
			return false;
		}

		if (getInitiator() == null) {
			if (other.getInitiator() != null) {
				return false;
			}
		}
		else if (!getInitiator().equals(other.getInitiator())) {
			return false;
		}

		return true;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.IApprovable#getApprovalStatus()
	 */
	@Override
	public EApprovalStatus getApprovalStatus() {
		return approvalStatus;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.ICommented#getComment()
	 */
	@Override
	public String getComment() {
		return comment;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.s.IUserConnection#getConnection()
	 */
	@Override
	public IUser getRecipient() {
		return recipient;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.s.IUserConnection#getUser()
	 */
	@Override
	public IUser getInitiator() {
		return initiator;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.impl.DataObjectImpl#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;

		result = prime * result + (getRecipient() == null ? 0 : getRecipient().hashCode());
		result = prime * result + (getInitiator() == null ? 0 : getInitiator().hashCode());

		return result;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.IBasicDataObject#init()
	 */
	@Override
	public void init() throws Exception {
		super.init();

		comment = null;
		initiator = null;
		recipient = null;
		approvalStatus = EApprovalStatus.NONE;
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.infodavid.common.model.IApprovable#setApprovalStatus(org.infodavid.common.model.IApprovable
	 * .EApprovalStatus)
	 */
	@Override
	public void setApprovalStatus(final EApprovalStatus value) {
		if (!equals(approvalStatus, value)) {
			final Object old = approvalStatus;
			approvalStatus = value;
			markModified(APPROVAL_STATUS, old, approvalStatus);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.ICommented#setComment(java.lang.String)
	 */
	@Override
	public void setComment(final String value) {
		if (!equals(comment, value)) {
			final Object old = comment;
			comment = value;
			markModified(COMMENT, old, comment);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.s.IUserConnection#setConnection(org.infodavid.common
	 * .model.s.IUser)
	 */
	@Override
	public void setRecipient(final IUser value) {
		if (!equals(recipient, value)) {
			final Object old = recipient;
			recipient = value;
			markModified(RECIPIENT, old, recipient);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.s.IUserConnection#setUser(org.infodavid.common.model .s.IUser)
	 */
	@Override
	public void setInitiator(final IUser value) {
		if (!equals(initiator, value)) {
			final Object old = initiator;
			initiator = value;
			markModified(INITIATOR, old, initiator);
		}
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#toString(java.lang.StringBuffer)
	 */
	@Override
	protected StringBuffer toString(final StringBuffer builder) {
		super.toString(builder);

		builder.append("[comment=");
		builder.append(comment);
		builder.append(", initiator=");

		if (initiator == null) {
			builder.append("null");
		}
		else {
			builder.append(initiator.getKey());
		}

		builder.append(", recipient=");

		if (recipient == null) {
			builder.append("null");
		}
		else {
			builder.append(recipient.getKey());
		}

		builder.append(", approvalStatus=");
		builder.append(approvalStatus);
		builder.append("]");

		return builder;
	}
}
